#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe
#
# Distributed under terms of the MIT license.
from esr_parser import shortcuts
import json
from django.core.cache import cache
# from gevent.pool import Pool
# import gevent
# from gevent.queue import Queue, Empty
import requests
# from gevent import monkey
import urllib
from esr_parser import core


def get_world_pc_ranking(job):
    return shortcuts.get_world_pc_ranking(**job.task.confdata)


def get_pc_ranking(job):
    return shortcuts.get_pc_ranking(**job.task.confdata)


def get_mobile_ranking(job):
    return shortcuts.get_mobile_ranking(**job.task.confdata)


def get_world_mobile_ranking(job):
    return shortcuts.get_world_mobile_ranking(**job.task.confdata)


def get_custom_items_detail(job):
    item_list = job.task.confdata.get('items')

    resp = requests.get(item_list)
    lines = resp.content.split('\n')

    total_count = len(lines)
    error_count = 0
    success_count = 0

    job_task = job.task
    job_task.confdata.get('limit', 84)

    result = ""
    for line in lines[:84]:
        try:
            product_info = json.loads(line)
            product_sku = product_info['pro_sku']
            tmall = 'tmall' in product_info['pro_url']
            data = shortcuts.item_detail(product_sku, tmall)
            result += json.dumps(data, ensure_ascii=False)
            result += "\n"
        except Exception as e:
            job.logger.error(e)
            error_count += 1
        else:
            success_count += 1

    job.logger.info(json.dumps(
        {"total": total_count, "error": error_count, "success": success_count}))
    return result


def get_ranking_item_detail(job):
    ranking_job = job.dependence.all()[0]
    url = ranking_job.outputs.get(key='items').value.url
    url = urllib.unquote(url)
    ranking_data = requests.get(url).content.strip()
    lines = ranking_data.split('\n')
    conf = job.task.confdata
    comments = int(conf['comments'])

    result = ""
    infos = [json.loads(line) for line in lines][:conf.get('limit', 84)]
    pids = [(info['auction_num_id'], info['is_tm']) for info in infos]

    result, info = download_items(job, pids, comments)

    job.logger.info(json.dumps(info))
    return result


def get_product_list_items(job):
    conf = job.task.productlist.all()[0]
    product_list = conf.product_list.strip().split('\n')
    comment_limit = conf.comment_limit

    pids = []
    for info in product_list:
        is_tmall, p_id = info.split(',')
        p_id = p_id.strip()
        is_tmall = int(is_tmall)

        pids.append((p_id, is_tmall))

    result, info = download_items(job, pids, comment_limit)
    job.logger.info(json.dumps(info))
    return result


def download_items(job, pids, comment_limit=200):
    print 'prepare job'
    success = []
    error = []
    for product_id, is_tmall in pids:
        try:
            # item cache

            cache_key = 'item_detail_{}_{}'.format(product_id, comment_limit)
            data = cache.get(cache_key)
            if not data:
                data = shortcuts.item_detail(
                    product_id, is_tmall, comment_limit)
                cache.set(cache_key, data, 86400)
            if data:
                data_str = json.dumps(data, ensure_ascii=False)
                success.append(data_str)
        except Exception as e:
            error.append((product_id, e))
            job.logger.error('parser item: {} is_tm: {} error'.format(product_id, is_tmall))
            job.logger.error(e)

    return "\n".join(success), {"total": len(pids), "success_count": len(success), "error": len(error)}
